package com.ruge.test.springMVC.shiro;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

import javax.sql.DataSource;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2018-07-02  22:20
 * 描述 ：
 */
public class demo01_03_JDBCRealm测试 {
    @Test
    public void test(){

        JdbcRealm jdbcRealm = new JdbcRealm();
        DruidDataSource dataSource = new DruidDataSource() ;
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/db_mybatis?serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        jdbcRealm.setDataSource(dataSource);
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(jdbcRealm);
        /**
         * 主体提交认证请求
         */
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zhang", "123");
        subject.login(usernamePasswordToken);

        boolean authenticated = subject.isAuthenticated();
        System.out.println(authenticated);

        boolean admin = subject.hasRole("admin");
        System.out.println(admin);

    }
}
